
/**
 * 惰性函数包装类
 * @param {Function} makeHandle - 判断函数，返回值为数值，对应类构造参数索引。
 * @param {...Function} ...handles - 任意函数
 * @returns {Function} sluggishHandle
 */
class Sluggish {

    #target;

    constructor(...args) {

        args.forEach((arg, index) => {
            if (typeof arg !== 'function') {
                throw new TypeError(`Argument at index ${index} is not a function`);
            }
        });

        const count = args[0]();

        if (typeof count !== 'number' || isNaN(count)) {
            throw TypeError('The provided function must return a valid number');
        }

        this.#target = args[count];
        this.#target._targetArguments = args;
        return this.#target;

    }

    /**
     * 重新走一遍条件判断并返回新函数
     * @param {Function} sluggishHandle 
     * @returns {Function} sluggishHandle
     */
    static reset(sluggishHandle) {
        if (sluggishHandle._targetArguments) {
            return new Sluggish(...sluggishHandle._targetArguments);
        }
        throw Error('The provided function is not returned by this class.');
    }

}
